﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace LDBUtilities
{
    public class DevicePower
    {
        public const UInt32 POWER_FORCE = 4096;
        public const UInt32 POWER_NAME = 1;

        public const int POWER_STATE_ON = 0x10000;
        public const int POWER_STATE_OFF = 0x20000;
        public const int POWER_STATE_SUSPEND = 0x200000;

        public enum DevicePowerState : int
        {
            Unspecified = -1,
            D0 = 0,
            D1 = 1,
            D2 = 2,
            D3 = 3,
            D4 = 4,
        }

        [DllImport("coredll", SetLastError = true)]
        extern public static IntPtr SetPowerRequirement([MarshalAs(UnmanagedType.LPWStr)] string pvName, DevicePowerState DeviceState, UInt32 DeviceFlags, [MarshalAs(UnmanagedType.LPWStr)] string pvSystemState, UInt32 StateFlags);

        [DllImport("coredll", SetLastError = true)]
        extern public static UInt32 ReleasePowerRequirement(IntPtr handle);

        public const int PPN_REEVALUATESTATE = 0x0001;
        public const int PPN_POWERCHANGE = 0x0002;
        public const int PPN_UNATTENDEDMODE = 0x0003;
        public const int PPN_SUSPENDKEYPRESSED = 0x0004;
        public const int PPN_POWERBUTTONPRESSED = 0x0004;
        public const int PPN_SUSPENDKEYRELEASED = 0x0005;
        public const int PPN_APPBUTTONPRESSED = 0x0006;

        [DllImport("coredll")]
        public extern static bool PowerPolicyNotify(int dwMessage, int dwData);

        /// <summary>
        /// Set the power state of device.
        /// </summary>
        /// <param name="psState">Names the desired system state to enter. If this parameter is not NULL then the StateFlags parameter is ignored.</param>
        /// <param name="stateFlags">
        /// POWER_STATE_ON	On state	
        /// POWER_STATE_OFF	Off state	
        /// POWER_STATE_CRITICAL	Critical state	
        /// POWER_STATE_BOOT	Boot state	
        /// POWER_STATE_IDLE	Idle state	
        /// POWER_STATE_SUSPEND	Suspend state	
        /// POWER_STATE_RESET	Reset state</param>
        /// <param name="options">Uses the optional POWER_FORCE flag to indicate that the state transfer is urgent.</param>
        /// <returns></returns>
        [DllImport("coredll.dll", EntryPoint = "SetSystemPowerState", SetLastError = true)]
        public static extern int SetSystemPowerState(string psState, int stateFlags, UInt32 options);

        [DllImport("coredll", EntryPoint = "SetDevicePower")]
        public static extern IntPtr SetDevicePower(string pvDevice, UInt32 dwDeviceFlags, DevicePowerState DeviceState);

        /// <summary>
        /// Run app at a given time, even if in standby mode.
        /// </summary>
        /// <param name="pwszAppName"></param>
        /// <param name="lpTime"></param>
        /// <returns></returns>
        [DllImport("coredll.dll", EntryPoint = "CeRunAppAtTime", SetLastError = true)]
        public static extern bool CeRunAppAtTime(string pwszAppName, byte[] lpTime);


    }
}
